package com.topisv.tms.web;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Maps;
import com.topisv.tms.entity.AreaGroup;
import com.topisv.tms.entity.Customers;
import com.topisv.tms.entity.JSONParam;
import com.topisv.tms.entity.PerformanceFee;
import com.topisv.tms.entity.QueryParamHolder;
import com.topisv.tms.service.AreaGroupService;
import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.PerformanceFeeService;
import com.topisv.tms.service.account.AccountService;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;
import com.topisv.tms.web.base.BaseDataTableController;

/**
 * 
 * @author 
 */
@Controller
@RequestMapping(value = "/performanceFee")
public class PerformanceFeeController extends BaseDataTableController{

	@Autowired
	private AccountService accountService;
	
	@Autowired
	private AreaGroupService areaGroupService;
	
	@Autowired
	private CustomersService customersService;


	@Autowired
	private PerformanceFeeService performanceFeeService;

	@RequestMapping(value = "v2_show")
	public String list(Model model,  ServletRequest request) {
		ShiroUser curUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		List<AreaGroup> warehouseList = areaGroupService.findByAreaGroups(curUser.areagroups);
		model.addAttribute("warehouseList", warehouseList);
		return "performance/performanceFeeList";
	}
	
	@RequestMapping(value = "page",method = RequestMethod.POST)
	public void page(@RequestBody JSONParam[] paramJson, HttpServletRequest request,HttpServletResponse response){
		try {
			// 客户端参数
			QueryParamHolder paramHolder = collectParam(paramJson);
			//paramHolder.getSearchParams().put("EQ_status", "1");
			Page<PerformanceFee> page = performanceFeeService.getPerformanceFee(paramHolder);
			// 封装查询结果
			response.getWriter().print(bulidJSON(page));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "v2_create", method = RequestMethod.GET)
	public String createForm(Model model) {
		ShiroUser curUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		List<AreaGroup> warehouseList = areaGroupService.findByAreaGroups(curUser.areagroups);
		List<Customers> customerList=this.customersService.loadCustomerByAuthority(curUser);
		model.addAttribute("warehouseList", warehouseList);
		model.addAttribute("customerList", customerList);
		model.addAttribute("performanceFee", new PerformanceFee());
		model.addAttribute("action", "create");
		return "performance/performanceFeeForm";
	}
	
	
	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(@RequestParam(value = "optype", required = true) String optype,
			@RequestParam(value = "warehouseCode", required = true) String warehouseCode,
			@RequestParam(value = "fee", required = true) String fee,
			@RequestParam(value = "customerCode", required = true) String customerCode,
			RedirectAttributes redirectAttributes) throws ParseException {
		PerformanceFee performanceFee = performanceFeeService.findPerformanceFee(warehouseCode, customerCode, optype);
		if(null == performanceFee)
		{
			performanceFee = new PerformanceFee();
			performanceFee.setWarehouseCode(warehouseCode);
			performanceFee.setCustomerCode(customerCode);
			performanceFee.setOptype(optype);
			performanceFee.setFee(Float.parseFloat(fee));
			performanceFeeService.savePerformanceFee(performanceFee);
			redirectAttributes.addFlashAttribute("message", "创建成功");
		}
		else
		{
			redirectAttributes.addFlashAttribute("message", "已存在");
		}
		
		return "redirect:/performanceFee/v2_show";
	}


	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
		performanceFeeService.deletPeformanceFee(id);
		redirectAttributes.addFlashAttribute("message", "删除s成功");
		return "redirect:/performanceFee/v2_show";
	}
	
	/**
	 * 使用@ModelAttribute, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出Task对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此本方法在该方法中执行.
	 */
	@ModelAttribute("preloadPerformanceFee")
	public PerformanceFee getPerformanceFee(@RequestParam(value = "id", required = false) Long id) {
		if (id != null) {
			return performanceFeeService.getPerformanceFee(id);
		}
		return null;
	}
	/**
	 * Ajax请求校验loginName是否唯一。
	 */
	@RequestMapping(value = "checkRealname")
	@ResponseBody
	public String checkLoginName(@RequestParam("realname") String realname) {
		if (accountService.findUserByRealname(realname) == null) {
			return "false";
		} else {
			return "true";
		}
	}
}